#include "StdAfx.h"
#include "Example.h"

void Example::initialise( HWND hWnd )
{
	mRoot = Root::getSingletonPtr();
	mRoot->initialise(hWnd);

	mSceneMgr = (SceneManager*)mRoot->createSceneManager(L"sceneMgr");
	mCamera = mSceneMgr->getMainCamera();

	Vector3 position(0.0f, 0.0f, 0.0f);
	Vector3 target = Vector3::UNIT_Z;
	Vector3 up(0.0f, 1.0f, 0.0f);

	SceneNode* camNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(L"CamNode");
	camNode->attachObject(mCamera);
	camNode->setWorldSpacePosition(position);

	mCamera->setPosition(position);
	mCamera->setLookAt(target);
	mCamera->setUp(up);

	//camNode->setWorldSpacePosition(0, 0, 10);
	//camNode->yaw(Radian(30));
	mCamera->update();

	mLight = mSceneMgr->createLight(L"light");
	mLight->setType(D3DLIGHT_DIRECTIONAL);
	mLight->setDiffuseColour(1.0, 1.0, 1.0);

	mSceneMgr->setAmbientLight(0.0, 0.0, 0.0);

	ResourceManager::getSingletonPtr()->addLocation(L"../media");

	mInput = OISManager::getSingletonPtr();
	mInput->initialise((size_t)hWnd);
	mInput->setCamera(mCamera);
	mRoot->addFrameListener(mInput);

	createScene();
}

void Example::release()
{
	if (mRoot)
	{
		mRoot->release();
		mRoot = NULL;
	}
}

BOOL Example::renderOneFrame()
{
	if (!mRoot)
	{
		return FALSE;
	}
	return mRoot->renderOneFrame();
}

void Example::createScene()
{
	RenderPriorityGroup rpg;
	rpg.mShaderFileName = L"basic_shader.hlsl";
	rpg.mShaderClassName = "BasicShader";

	mSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(L"node");
	mSceneNode2 = mSceneMgr->getRootSceneNode()->createChildSceneNode(L"node2");

	mEntity = new Entity(L"ent");
	mEntity->createFromXFile(L"tiger.x", L"Tiger", rpg);

	mSceneNode->attachObject(mEntity);
	mSceneNode->yaw(Radian(PI / 16.0f));
	mSceneNode->setWorldSpacePosition(-2, 0, 0);

	mEntity2 = new Entity(L"ent2");
	mEntity2->createFromXFile(L"airplane 2.x", L"Air", rpg);

	mSceneNode2->attachObject(mEntity2);
	mSceneNode2->yaw(Radian(PI / 2.0f));
	mSceneNode2->setWorldSpacePosition(10, 0, 0);

	//LightShaderManager::getSingletonPtr()->createEffect(L"phone.hlsl", "PhoneLight");
	mSceneMgr->setSky(L"cloudy_noon.dds");
}
